package cn.kawa.order.controller;

import cn.kawa.basic.util.LoginContext;
import cn.kawa.order.dto.ConfirmDto;
import cn.kawa.order.service.IOrderPetAcquisitionService;
import cn.kawa.order.domain.OrderPetAcquisition;
import cn.kawa.order.query.OrderPetAcquisitionQuery;
import cn.kawa.basic.util.JsonResult;
import cn.kawa.basic.util.PageList;
import cn.kawa.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口： 重构接口用于取消订单
     * @param id
     * @return JsonResult 响应给前端
     */
    @DeleteMapping(value="/{id}")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("取消失败");
        }
    }

    /**
     * 接口：订单确认，确认收购
     * @param confirmDto
     * @return JsonResult 响应给前端
     */
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody ConfirmDto confirmDto){
        try {
            orderPetAcquisitionService.confirm(confirmDto);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    /**
     * 接口：分页查询或高级查询, 重构用于权限后的显示
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
        LoginInfo loginUser = LoginContext.getLoginUser(request);
        return orderPetAcquisitionService.queryPage(query,loginUser);
    }

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return JsonResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if( orderPetAcquisition.getId()!=null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.add(orderPetAcquisition);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }



    /**
    * 接口：批量删除
    * @param ids
    * @return JsonResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
                orderPetAcquisitionService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition findOne(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> findAll(){
        return orderPetAcquisitionService.findAll();
    }



}
